package com.ln.prac.base.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * <pre>
 *  Java 代理
 * </pre>
 *
 * @Author zhangj
 * @Date 2022/6/18 23:07
 */
public class ProxyTest {

    interface SellTicket {
        void sell();
    }

    static class TranStation implements SellTicket {

        @Override
        public void sell() {
            System.out.println("火车站卖票");
        }
    }

    static class ProxyFactory {
        private TranStation tranStation = new TranStation();
        public SellTicket getProxyObject() {
            SellTicket sellTicket = (SellTicket) Proxy.newProxyInstance(TranStation.class.getClassLoader(), TranStation.class.getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("代理车站...");
                    Object re = method.invoke(tranStation, args);
                    return re;
                }
            });
            return sellTicket;
        }
    }

    public static void main(String[] args) {
        ProxyFactory fac = new ProxyFactory();
        SellTicket proxyObject = fac.getProxyObject();
        proxyObject.sell();

        System.out.println(proxyObject.getClass());

        while (true) {}

    }


}
